package com.lims.admin.action;


import cn.org.rapid_framework.web.scope.Flash;

import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.ModelDriven;


import javacommon.base.*;

import cn.org.rapid_framework.page.*;

import com.lims.admin.model.*;
import com.lims.admin.service.*;
import com.lims.admin.vo.query.*;

/**
 * @author zzh
 */
@SuppressWarnings({"serial","unchecked"})
public class MenuAction extends BaseStruts2Action implements Preparable,ModelDriven{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	//forward paths
	protected static final String QUERY_JSP = "/pages/admin/Menu/query.jsp";
	protected static final String LIST_JSP= "/pages/admin/Menu/list.jsp";
	protected static final String CREATE_JSP = "/pages/admin/Menu/create.jsp";
	protected static final String EDIT_JSP = "/pages/admin/Menu/edit.jsp";
	protected static final String SHOW_JSP = "/pages/admin/Menu/show.jsp";
	//redirect paths,startWith: !
	protected static final String LIST_ACTION = "!/pages/admin/Menu/list.do";
	
	private MenuManager menuManager;
	
	private Menu menu;
	java.lang.String id = null;
	private String items;

	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			menu = new Menu();
		} else {
			menu = (Menu)menuManager.getById(id);
		}
	}
	
	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,注意大小写 */
	public void setMenuManager(MenuManager manager) {
		this.menuManager = manager;
	}	
	
	public Object getModel() {
		return menu;
	}
	
	public void setObjid(java.lang.String val) {
		this.id = val;
	}

	public void setItems(String items) {
		this.items = items;
	}
	
	/** 执行搜索 */
	public String list() {
		MenuQuery query = newQuery(MenuQuery.class,DEFAULT_SORT_COLUMNS);
		Page page = menuManager.findPage(query);
		savePage(page,query);
		this.getRequest().setAttribute("menuList", menuManager.findAll());
		return LIST_JSP;
	}
	
	/** 保存新增对象 */
	public String save() {
		//菜单父ID为空串时，必须设置null
		String pid = menu.getParentid();
		if(pid==null || "".equals(pid)){
			menu.setParentid(null);
		}
		//设置 级别为1时，菜单是否为叶子属性为0，否为1
		int level = this.menu.getMenulevel();
		if(level == 1) {
			this.menu.setIsleaf(0);
		} else {
			this.menu.setIsleaf(1);
		}
		menuManager.save(menu);
		//取得页数
		String pn = this.getRequest().getParameter("pageNumber");
		String ps = this.getRequest().getParameter("pageSize");
		return LIST_ACTION+"?pageNumber="+pn+"&pageSize="+ps;
	}
	
	/**保存更新对象*/
	public String update() {
		//设置 级别为1时，菜单是否为叶子属性为0，否为1
		int level = this.menu.getMenulevel();
		if(level == 1) {
			this.menu.setIsleaf(0);
		} else {
			this.menu.setIsleaf(1);
		}
		menuManager.update(this.menu);
		Flash.current().success(UPDATE_SUCCESS);
		//取得页数
		String pn = this.getRequest().getParameter("pageNumber");
		String ps = this.getRequest().getParameter("pageSize");
		return LIST_ACTION+"?pageNumber="+pn+"&pageSize="+ps;
	}
	
	/**删除对象*/
	public String delete() {
		String[] objids = items.split(",");
		for (int i = 0; i < objids.length; i++) {
			menuManager.removeAndcascadeById(objids[i]);
		}
		//取得页数
		String pn = this.getRequest().getParameter("pageNumber");
		String ps = this.getRequest().getParameter("pageSize");
		return LIST_ACTION+"?pageNumber="+pn+"&pageSize="+ps;
	}

}
